﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using memo;
using memoVM;

namespace memoUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class TopicSelectorWindow : Window
    {
        public TopicSelectorWindow()
        {
            InitializeComponent();

            _ViewModel = ViewModel.Instance;
            NumberOfTopics = _ViewModel.GetNumberOfTopics();

            DynamicStackPanel = new StackPanel[NumberOfTopics];
            DynamicLabel = new Label[NumberOfTopics];
            DynamicImage = new Image[NumberOfTopics];

            DynamicProgressBarPanel = new Grid[NumberOfTopics];
            DynamicProgressBar = new ProgressBar[NumberOfTopics];
            DynamicProgressBarText = new TextBlock[NumberOfTopics];

            Init();
        }

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            _ViewModel.Exit();
            Close();
        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {
            TopicEditorWindow Window = new TopicEditorWindow(true);
            Window.ShowDialog();
            TopicSelectorWindow window = new TopicSelectorWindow();
            window.Show();
            Close();
        }

        private void TopicRight_Click(object sender, MouseButtonEventArgs e)
        {
            //konverzió hogy elérjük a tag propertyt amiben a topic sorszámát tároljuk 
            StackPanel obj = sender as StackPanel;
            _ViewModel.SelectedTopic = int.Parse(obj.Tag.ToString());

            ContextMenu cm = this.FindResource("cmButton") as ContextMenu;
            cm.PlacementTarget = sender as StackPanel;
            cm.IsOpen = true;
        }

        private void TopicLeft_Click(object sender, MouseButtonEventArgs e)
        {
            //konverzió hogy elérjük a tag propertyt amiben a topic sorszámát tároljuk 
            StackPanel obj = sender as StackPanel;
            _ViewModel.SelectedTopic = int.Parse(obj.Tag.ToString());
            SubTopicSelectorWindow Window = new SubTopicSelectorWindow();
            Window.Show();
            this.Close();
        }

        private void TopicEdit_Click(object Sender, RoutedEventArgs e)
        {
            TopicEditorWindow Window = new TopicEditorWindow(false);
            Window.ShowDialog();
            TopicSelectorWindow window = new TopicSelectorWindow();
            window.Show();
            Close();
        }

        private void TopicDelete_Click(object Sender, RoutedEventArgs e)
        {
            _ViewModel.DeleteSelectedTopic();
            TopicSelectorWindow window = new TopicSelectorWindow();
            window.Show();
            Close();
        }

        internal void Init()
        {
            int progressBarHeight = 25;
            int labelHeight = 40;
            int labelFontSize = 20;
            Topic myTopic;

            //a szükséges sorok számának kiszámítása
            NumberOfRows = (int)Math.Ceiling(((double)NumberOfTopics / NumberOfColumns));  // ¯\(ツ)/¯

            RowDefinition[] rowDef = new RowDefinition[NumberOfRows];

            //a szükséges gird sorok létrehozása
            for (int i = 4; i < NumberOfRows; i++)
            {
                rowDef[i] = new RowDefinition();
                rowDef[i].Height = new GridLength(1, GridUnitType.Star);
                TopicGrid.RowDefinitions.Add(rowDef[i]);
            }

            //topikokhoz tartozó controlok létrehozása
            for (int i = 0; i < NumberOfTopics; i++)
            {
                myTopic = _ViewModel.GetTopicAtIndex(i);
                BrushConverter bc = new BrushConverter();

                //Topichoz tartozó controlokat tartalmazó panel beállítása
                DynamicStackPanel[i] = new StackPanel();
                DynamicStackPanel[i].Background = (Brush)bc.ConvertFromString("#ACD661"); 
                //DynamicStackPanel[i].Background = Brushes.PapayaWhip; 
                DynamicStackPanel[i].Tag = i; // topic sorszámának letárolása a hozzá tartozó subtopic betöltéséhez
                DynamicStackPanel[i].Margin = new Thickness(8); // topic panelek közötti távolság
                DynamicStackPanel[i].Orientation = Orientation.Vertical;
                DynamicStackPanel[i].MouseLeftButtonDown += TopicLeft_Click;
                DynamicStackPanel[i].MouseRightButtonDown += TopicRight_Click;
                Grid.SetRow(DynamicStackPanel[i], i / (TopicGrid.ColumnDefinitions.Count));
                Grid.SetColumn(DynamicStackPanel[i], i % (TopicGrid.ColumnDefinitions.Count));

                //Topicnak a nevéhez tartozó label beállítása
                DynamicLabel[i] = new Label();
                DynamicLabel[i].Content = myTopic.Title; //Itt kell megadni a topic nevét //string
                DynamicLabel[i].Height = labelHeight;
                DynamicLabel[i].FontSize = labelFontSize;
                DynamicLabel[i].FontWeight = FontWeights.Bold;
                DynamicLabel[i].Foreground = Brushes.White;
                DynamicLabel[i].VerticalAlignment = VerticalAlignment.Top;
                DynamicLabel[i].HorizontalAlignment = HorizontalAlignment.Center;
                DynamicStackPanel[i].Children.Add(DynamicLabel[i]);

                //Topicnak az ikonjának beállítása
                DynamicImage[i] = new Image();
                //string path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "/Images/ASD" + rnd.Next(0, 2) + ".jpg"; //(╯°□°）╯︵ ┻━┻  //ez csak próba, ↓ ezt használd 
                string path = ImagePath + myTopic.Icon;  //kép betöltése a topichoz // EZT HASZNÁLD!!!  //stringben a képhez vezető path // ┬─┬﻿ ノ(゜-゜ノ)
                //DynamicImage[i].Source = (ImageSource)new ImageSourceConverter().ConvertFromString(path); 
                DynamicImage[i].Source = (ImageSource)ImageLoader(path);
                DynamicImage[i].Margin = new Thickness(5, 0, 5, 5);
                DynamicImage[i].Stretch = Stretch.Fill;
                DynamicImage[i].VerticalAlignment = VerticalAlignment.Center;
                DynamicImage[i].HorizontalAlignment = HorizontalAlignment.Center;
                DynamicImage[i].ToolTip = myTopic.ShortDescription;
                DynamicStackPanel[i].Children.Add(DynamicImage[i]);

                //Progressbart tartalmazó panel létrehozása
                DynamicProgressBarPanel[i] = new Grid();
                DynamicProgressBarPanel[i].Margin = new Thickness(5, 0, 5, 5);
                DynamicProgressBarPanel[i].HorizontalAlignment = HorizontalAlignment.Stretch;
                DynamicProgressBarPanel[i].VerticalAlignment = VerticalAlignment.Bottom;

                //Progressbar létrehozása
                DynamicProgressBar[i] = new ProgressBar();
                DynamicProgressBar[i].Minimum = 0;
                DynamicProgressBar[i].Maximum = 100;
                DynamicProgressBar[i].Value = myTopic.GetKnowledgePercent();  //itt kell értéket adni a progressbarnak // int 0-100 intervallumon
                DynamicProgressBar[i].Height = progressBarHeight;
                DynamicProgressBar[i].VerticalAlignment = VerticalAlignment.Stretch;
                DynamicProgressBar[i].HorizontalAlignment = HorizontalAlignment.Stretch;
                DynamicProgressBarPanel[i].Children.Add(DynamicProgressBar[i]);

                //Progressbar feliratának beállítása //itt nem kell állítani semmit sem
                DynamicProgressBarText[i] = new TextBlock();
                DynamicProgressBarText[i].Text = DynamicProgressBar[i].Value.ToString() + "%";
                DynamicProgressBarText[i].TextAlignment = TextAlignment.Center;
                DynamicProgressBarText[i].Height = progressBarHeight;
                DynamicProgressBarText[i].VerticalAlignment = VerticalAlignment.Stretch;
                DynamicProgressBarText[i].HorizontalAlignment = HorizontalAlignment.Stretch;
                DynamicProgressBarPanel[i].Children.Add(DynamicProgressBarText[i]);

                DynamicStackPanel[i].Children.Add(DynamicProgressBarPanel[i]);
                DynamicStackPanel[i].Cursor = Cursors.Hand;
                TopicGrid.Children.Add(DynamicStackPanel[i]);
            }
        }

        public object ImageLoader(string path)
        {
            var bitmap = new BitmapImage();
            var stream = File.OpenRead(path);
            bitmap.BeginInit();
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.StreamSource = stream;
            bitmap.EndInit();
            stream.Close();
            stream.Dispose();
            return bitmap;
        }

        private string ImagePath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "/Images/";

        private int NumberOfTopics; //topic darabszámának a megadása        
        private const int NumberOfColumns = 4; // oszlopok száma
        private int NumberOfRows; // sorok száma

        private StackPanel[] DynamicStackPanel;  //Egy topichoz tartozó controlokat tartalmazó panel
        private Label[] DynamicLabel;  //topic neve
        private Image[] DynamicImage; //topichoz tartozó ikon

        private Grid[] DynamicProgressBarPanel; //Progressbarok és a hozzájuk tartozó feliratok tartalmazó panel
        private ProgressBar[] DynamicProgressBar; //topicok tudási szintjét jelző progressbar
        private TextBlock[] DynamicProgressBarText; //progressbarhoz tartozó felirat megjelenítése szolgáló textblock

        private ViewModel _ViewModel;

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
